/**
 * $Id$
 *
 * @author Noritaka Horio<holy.shared.design@gmail.com>
 * @copyright 2009 Noritaka Horio all rights resrved. 
 * @license The MIT License
 * @version $Revision$
 * @since
 */

var Clock = new Class({

	Implements: [Options],

	options: {
		"id": "processing",
		"swf": "app:/libraries/processing/processing.swf"
	},

	initialize: function(container, schedule, options) {
		this.setOptions(options);
		this.container = $(container);
		this.starting = false;
		this.loaded = false;

		var newSchedule = new Hash();
		schedule.each(function(handler, key) {
			newSchedule.set(key, handler.bind(this));
		}.bind(this));

		this.scheduler = new Scheduler(newSchedule);
	},

	addEvents: function(events) {
		var newEvents = {};
		var s = new Hash(events);
		s.each(function(func, key) {
			newEvents[key] = func.bind(this);
		}.bind(this));
		this.scheduler.addEvents(newEvents);
	},

	addEvent: function(type, func) {
		this.scheduler.addEvent(type, func.bind(this));
	},

	stop: function() {
		this.starting = false;
		this.scheduler.stop();
	},

	start: function() {
		if (this.loaded && !this.starting) {
			this.starting = true;
			this.scheduler.start();
		} else {
			this.processing = new Processing(this.container, {
				"id": this.options.id,
				"swf": this.options.swf,
				"onLoad": function() {
					this.loaded = true;
					this.starting = true;
					this.scheduler.start();
				}.bind(this)
			});
		}
	}
});
